<template>
  <div class="bac">
    <div class="register">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <p class="tit">
          欢迎注册账号
        </p>
        <el-form-item prop="checkName">
          <el-input
            v-model="ruleForm.checkName"
            placeholder="输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
            placeholder="确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align:center;padding-left:20px">
          <el-button
            type="primary"
            @click="register('ruleForm')"
          >注册</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>
<script>
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('账号不能为空'))
      }
      setTimeout(() => {
        if (value.length < 6) {
          callback(new Error('账号必须大于6位'))
        } else {
          if (this.ruleForm.msg) {
            callback(new Error('该账户已存在'))
            this.ruleForm.msg = ''
          }
        }
        callback()
      }, 1000)
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (value.length < 6) {
          callback(new Error('密码必须大于6位'))
        } else if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pass: '',
        checkPass: '',
        checkName: '',
        msg: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        checkName: [
          { validator: checkName, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async register(formName) {
      const userObj = {
        username: this.ruleForm.checkName,
        password: this.ruleForm.pass
      }
      console.log(userObj)
      const res = await this.$http.register(userObj)
      console.log(res.message)
      if (res.message === '该账户已存在') {
        this.ruleForm.msg = '该账户已存在'
      }
      console.log(this.ruleForm.msg)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            message: '注册成功',
            type: 'success'
          })
          setTimeout(() => {
            this.$router.push('/')
          }, 1000)
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.bac {
  width: 100vw;
  height: 100vh;
  background-color: #eff8ff;
  margin-top: -60px;
  background: url(../img/bac3.png) no-repeat -300px 420px / 1920px 465px #eff8ff;;
}
.register {
  position: absolute;
  right: 0;
  bottom: 90px;
  top: 0;
  left: 0;
  height: 330px;
  margin: auto;
  width: 400px;
  padding-right: 100px;
  input::-webkit-input-placeholder {
    color: #bbb;
    font-weight: normal;
  }
  .sub {
    font-size: 18px;
    color: #fff;
    background: #3b78dd;
    height: 50px;
    margin-bottom: 30px;
    cursor: pointer;
  }
  .register {
    cursor: pointer;
  }
  .tit {
    margin-bottom: 20px;
    font-size: 24px;
    font-weight: bold;
    text-align: center;
    padding-left: 90px;
  }
}
</style>
